﻿using System;
using System.Collections.Generic;
using System.Data.SQLite;
using System.IO;
using System.Text;

namespace FID_LearningDataCollection
{
    /// <summary>
    /// 数据处理类
    /// </summary>
    public class Vankdata
    {
        private SQLiteConnection sqLiteConnection;
        private SQLiteCommand command;

        /// <summary>
        /// 压缩机控制数据的转换
        /// </summary>
        /// <param name="sqliteDatabase">原始sqlite数据库</param>
        /// <returns>（p1，Freq，deltaP）的数据列表</returns>
        public List<List<double>> GetCompressorData(SQLiteConnection sqLiteConnection)
        {
            /* ===================数据解析规则======================
             * 原始记录：
             * 采样时间 P1	     P2	   Freq
             *     t1   1.16	15.51	25
             *     t2   1.13	15.49	25
             *     t3   1.1	    15.48	25             
             *  ....
             *  解析方法和步骤：
             *  step0：采样时间递增排列，即t1<t2<t3,采用间隔时间一致；
             *  step1: 对同一个Freq，相邻两点做差：P1(t1)-P1(t2)=deltaP(t1)，deltaP>0；
             *  step2：用（p1(t1)，Freq(t1)，deltaP(t1)）构造一个List<double>数据；
             *  step3：对于不同的Freq，依次用相邻的点，构造List<List<double>>数据；
             *  step4：返回构造好的数据。
             ===========================end==========================*/
            this.sqLiteConnection= sqLiteConnection;

            // 2. 获取所有测试的频率
            List<double> freqs = GetFreqs();
            if (freqs == null || freqs.Count == 0)
            {
                throw new Exception("无法在数据库中获取到频率!");
            }

            // 3. 获取不同的频率的数据,组织成二维数组
            List<List<double>> freqsData = getFreqsData(freqs);

            return freqsData;
        }


        /// <summary>
        /// 获取所有的参与计算的频率
        /// </summary>
        /// <param name="sqliteDatabase"></param>
        /// <returns></returns>
        private List<double> GetFreqs()
        {
            string query = "SELECT DISTINCT Freq FROM StateHistory";
            List<double> freqs = new List<double>();
            using (SQLiteCommand command = new SQLiteCommand(query, this.sqLiteConnection))
            {
                using (SQLiteDataReader reader = command.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        freqs.Add(reader.GetDouble(0));
                    }
                }
            }
            return freqs;
        }

        /// <summary>
        /// 获取所有频率所包含的数据
        /// </summary>
        /// <param name="freqs">频率清单</param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        private List<List<double>> getFreqsData(List<double> freqs)
        {
            List<List<double>> freqsData = new List<List<double>>();

            foreach (var freq in freqs)
            {
                int clearCounter = 0;
                string query = "SELECT P1,Freq from StateHistory where Freq = " + freq + " and P1 > 1.0 order by SCTime";

                using (SQLiteCommand command = new SQLiteCommand(query, this.sqLiteConnection))
                {
                    using (SQLiteDataReader reader = command.ExecuteReader())
                    {
                        double currentP1 = 0.0, currentFreq = 0.0, deltaP1 = 0.0;
                        while (reader.Read())
                        {
                            // 抛弃2个数据
                            if (clearCounter < 4)
                            {
                                if (clearCounter == 3)
                                {
                                    currentP1 = reader.GetDouble(0);
                                    currentFreq = reader.GetDouble(1);
                                }
                                clearCounter++;
                                continue;
                            }
                            deltaP1 = (double)(Convert.ToDecimal(currentP1) - Convert.ToDecimal(reader.GetDouble(0)));

                            List<double> freqData = new List<double>();
                            if (currentP1 > 0 && currentFreq > 0 && deltaP1 > 0)
                            {
                                freqData.Add(currentP1);
                                freqData.Add(currentFreq);
                                freqData.Add(deltaP1);
                            }

                            // 记录供下轮使用
                            currentP1 = reader.GetDouble(0);
                            currentFreq = reader.GetDouble(1);

                            if (freqData != null && freqData.Count > 0)
                                freqsData.Add(freqData);
                        }
                    }
                }
                clearCounter = 0;

            }
            return freqsData;
        }
    }
}
